package com.wingscode.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.Query;
import com.wingscode.modules.common.entity.AgentEntity;
import com.wingscode.modules.common.service.AgentService;
import com.wingscode.modules.sys.dao.VipLevelDao;
import com.wingscode.modules.sys.entity.InquiryEntity;
import com.wingscode.modules.sys.entity.VipLevelEntity;
import com.wingscode.modules.sys.service.VipLevelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 会员等级
 *
 * @author yan
 */
@Service("vipLevelService")
public class VipLevelServiceImpl extends ServiceImpl<VipLevelDao, VipLevelEntity> implements VipLevelService {
	@Resource
	private AgentService agentService;

	@Override
    public PageUtils queryPage(Map<String, Object> params) {
		IPage<VipLevelEntity> page = this.page(
				new Query<VipLevelEntity>().getPage(params),
				new QueryWrapper<VipLevelEntity>()
						.eq("pid", 0)
						.orderByDesc("id")
		);

		if (page.getRecords() != null && page.getRecords().size() > 0) {
			for (VipLevelEntity vipLevel : page.getRecords()) {
				List<AgentEntity> agentList = agentService.list(
						new QueryWrapper<AgentEntity>()
								.eq("level", vipLevel.getId())
				);
				if (agentList != null && agentList.size() > 0)
					vipLevel.setPNum(agentList.size());
				else
					vipLevel.setPNum(0);
			}
		}

        return new PageUtils(page);
    }
	
	@Override
	public void delete(Long[] ids){
		this.removeByIds(Arrays.asList(ids));
	}

	@Override
	public List<VipLevelEntity> numList(Integer pid) { return this.baseMapper.numList(pid); }
}
